Skip to content

gh-94937: Allow len() to return large Python lengths - #150581

Open
cjc0013 wants to merge 3 commits into
python:mainfrom
cjc0013:gh-94937-len-overflow-fallback
Open

cjc0013 wants to merge 3 commits into
python:mainfrom
cjc0013:gh-94937-len-overflow-fallback

Conversation

@cjc0013

@cjc0013 cjc0013 commented May 29, 2026

Copy link
Copy Markdown

This implements the len() fallback discussed in gh-94937 while keeping the existing Py_ssize_t fast path.

The change adds an internal object-returning length helper. It first uses PyObject_Size() for the normal C protocol path, then falls back on OverflowError to the type-level __len__ special method and validates the result with PyNumber_Index() plus the existing non-negative check.

range now exposes a METH_COEXIST __len__ method that returns its stored Python integer length, so large ranges can participate in the fallback without changing PyObject_Size() or the C length protocol.

The optimized len() bytecode paths now use the same helper as the builtin, so callers such as random.choice() and random.sample() see the same behavior.

Tests and docs are updated for:

  • pure Python __len__ values larger than sys.maxsize
  • special-method lookup ignoring instance-level __len__
  • large range lengths
  • random.choice() and random.sample() over ranges larger than sys.maxsize

Tested with:

  • PYTHONPATH=./Lib:./Modules ./python.exe -m unittest -v test.test_builtin.BuiltinTest.test_len test.test_range.RangeTest.test_large_range test.test_random.MersenneTwister_TestBasicOps.test_choice test.test_random.MersenneTwister_TestBasicOps.test_sample_inputs
  • PYTHONPATH=./Lib:./Modules ./python.exe -m test test_builtin test_range test_random

@python-cla-bot

python-cla-bot Bot commented May 29, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@read-the-docs-community

read-the-docs-community Bot commented May 29, 2026 •

Copy link
Copy Markdown

@github-actions

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Aug 30, 2026
Resolve generated interpreter/JIT conflicts and ensure overflowing Python __len__ methods are evaluated once.
@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review stale Stale PR or inactive for long period of time. type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant